<?php
namespace addons\TfFutures\api\controllers;


use addons\TfFutures\common\models\Member;
use addons\TfFutures\common\models\Symbol;
use api\controllers\OnAuthController;
use common\helpers\ResultDataHelper;
use Yii;


class SelfCheckController extends OnAuthController
{
    public $modelClass = Symbol::class;

    /**
     * 不用进行登录验证的方法
     * 例如： ['index', 'update', 'create', 'view', 'delete']
     * 默认全部需要验证
     *
     * @var array
     */
    //protected $optional = ['recommend','get-one','sym-list'];

    /**
     * 添加自选
     */
    public function actionAdd(){
        if (!Yii::$app->request->isPost) {
            return ResultDataHelper::api(304,'access done!');
        }
        $uid = Yii::$app->user->identity->member_id;
        if (empty($uid)) {
            return ResultDataHelper::api(422,'用户id不能为空');
        }
        list($memberc,$member) = Member::member($uid);
        if (empty($memberc)) {
            return ResultDataHelper::api(422,'用户不存在');
        }
        $id = Yii::$app->request->post('symbol_id');
        if (empty($id)) {
            return ResultDataHelper::api(422,'添加的产品id不能为空');
        }
        $symbol = Symbol::findOne($id);
        if(empty($symbol)){
        	return ResultDataHelper::api(422,'产品不存在');
        }
        $symbolarr = explode(',',$member->symbol);
        if(in_array($id,$symbolarr)){
            return ResultDataHelper::api(422,'您已经添加过该产品');
        }
        $idarr = explode(',',$id);
        $newarr = array_unique(array_merge($symbolarr,$idarr));
        
          
        if($newarr[0]==null){
          	unset($newarr[0]);
          }
          //print_r($newarr);die;
        $member->symbol = implode(',',$newarr);
        if (!$member->save()) {
            return ResultDataHelper::api(422, $this->analyErr($member->getFirstErrors()));
        }
        return ResultDataHelper::api(200,'添加成功');
    }

    /**
     * 删除自选
     */
    public function actionDeleteCheck(){
        if (!Yii::$app->request->isPost) {
            return ResultDataHelper::api(304,'access done!');
        }
        $uid = Yii::$app->user->identity->member_id;
        if (empty($uid)) {
            return ResultDataHelper::api(422,'用户id不能为空');
        }
        $member = Member::find()->where(['member_id'=>$uid])->one();
        $id = Yii::$app->request->post('symbol_id');
        if (empty($id)) {
            return ResultDataHelper::api(422,'添加的产品id不能为空');
        }
        $arr = explode(',',$member->symbol);
        $delarr = explode(',',$id);
        $newarr = array_diff($arr,$delarr);
        $member->symbol = implode(',',$newarr);
        if (!$member->save()) {
            return ResultDataHelper::api(422, $this->analyErr($member->getFirstErrors()));
        }
        return ResultDataHelper::api(200,'删除成功');
    }

    /**
     * 自选列表
     */
    public function actionList()
    {
        if (!Yii::$app->request->isGet) {
            return ResultDataHelper::api(304, 'access done!');
        }
        $uid = Yii::$app->user->identity->member_id;
        if (empty($uid)) {
            return ResultDataHelper::api(422, '用户id不能为空');
        }
        list($memberc, $member) = Member::member($uid);
        if (empty($memberc)) {
            return ResultDataHelper::api(422, '用户不存在');
        }
        $model =  Symbol::find()->where(['in','id',explode(',',$member->symbol)])->asArray()->all();
//        print_r($model);die;
        return ResultDataHelper::api(200,'ok',$model);
    }
     /**
     * 更新排序
     * @return mixed
     */
    public function actionUpdateSort()
    {
        if (!Yii::$app->request->isPost) {
            return ResultDataHelper::api(403, 'Access Done');
        }
        $uid    = Yii::$app->user->identity->member_id;
        $member = Member::find()->where(['member_id'=>$uid])->one();
        if (empty($member)) {
            return ResultDataHelper::api(422, '用户不存在');
        }
        $member_arr = explode(',', $member->symbol);
        if (empty($member_arr)) {
            return ResultDataHelper::api(422, '请先添加自选产品');
        }
        $sort     = Yii::$app->request->post('sort');
        $sort_arr = explode(',', $sort);
        if (empty($sort_arr)) {
            return ResultDataHelper::api(422, '没有排序数据');
        }
        $s1 = sort($member_arr);
        $s2 = sort($sort_arr);
        // print_r($member_arr);die;
        if ($member_arr != $sort_arr) {
            return ResultDataHelper::api(422, '数据错误无法进行排序');
        }
        $member->symbol = $sort;

        if (!$member->save()) {
            return ResultDataHelper::api(422, '更新排序失败');
        }
        return ResultDataHelper::api(200, 'success');
    }
    
}